package com.viking.authentication.gui;

import com.viking.util.database.query.QueryInformationDataBase1;
import com.viking.util.dateselection.other.JTimeChooser;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Calendar;

import static javax.swing.SpringLayout.*;

public class MainWorkingWindow {

    private JFrame main;
    public MainWorkingWindow() throws SQLException, ClassNotFoundException {



        JFrame jf = new JFrame("捕精者");
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setSize(600, 500);
        JPanel jp = new JPanel();
        jf.setContentPane(jp);
        SpringLayout springLayout = new SpringLayout();
        jp.setLayout(springLayout);


        JLabel patient = new JLabel("患者：");
        jp.add(patient);
        patient.setForeground(Color.black);
        springLayout.putConstraint(NORTH, patient, 5, NORTH, jp);  //标签1北侧——>容器北侧
        springLayout.putConstraint(WEST, patient, 25, WEST, jp);    //标签1西侧——>容器西侧 10

        JTextField patient_text = new JTextField();
        jp.add(patient_text);
        patient_text.setPreferredSize(new Dimension (100,20));
        springLayout.putConstraint(NORTH, patient_text, 2, NORTH, jp);//文本框北侧——>容器北侧
        springLayout.putConstraint(WEST, patient_text, 0, EAST, patient);//文本框西侧——>主题标签东侧


        JLabel patient_id = new JLabel("患者编号：");
        jp.add(patient_id);
        patient.setForeground(Color.black);
        springLayout.putConstraint(NORTH, patient_id, 0, NORTH, patient_text);  //标签1北侧——>容器北侧
        springLayout.putConstraint(WEST, patient_id, 110, WEST, patient_text);    //标签1西侧——>容器西侧 10

        JTextField patient_id1 = new JTextField();
        jp.add(patient_id1);
        patient_id1.setPreferredSize(new Dimension (100,20));
        springLayout.putConstraint(NORTH, patient_id1, 0, NORTH, patient_id);//文本框北侧——>容器北侧
        springLayout.putConstraint(WEST, patient_id1, 0, EAST, patient_id);//文本框西侧——>主题标签东侧


        JLabel age = new JLabel("年龄：");
        jp.add(age);
        patient.setForeground(Color.black);
        springLayout.putConstraint(NORTH, age, 0, NORTH, patient_id1);  //标签1北侧——>容器北侧
        springLayout.putConstraint(WEST, age, 110, WEST, patient_id1);    //标签1西侧——>容器西侧 10

        JTextField age_text = new JTextField();
        jp.add(age_text);
        age_text.setPreferredSize(new Dimension (100,20));
        springLayout.putConstraint(NORTH, age_text, 0, NORTH, age);//文本框北侧——>容器北侧
        springLayout.putConstraint(WEST, age_text, 0, EAST, age);//文本框西侧——>主题标签东侧

        JLabel Gender = new JLabel("性别：");
        jp.add(Gender);
        Gender.setForeground(Color.black);
        springLayout.putConstraint(NORTH, Gender, 0, NORTH, age);  //标签1北侧——>容器北侧
        springLayout.putConstraint(WEST, Gender, 140, WEST, age);    //标签1西侧——>容器西侧 10

        JComboBox<String> c1 = new JComboBox<String>();
        jp.add(c1);
        c1.addItem("男");
        c1.addItem("女");
        springLayout.putConstraint(NORTH, c1, 0, NORTH, age_text);
        springLayout.putConstraint(WEST, c1, 35, EAST, age_text);
/*
        JLabel Admission_time = new JLabel("入院时间：");
        jp.add(Admission_time);
        Admission_time.setForeground(Color.black);
        springLayout.putConstraint(NORTH, Admission_time, 30, NORTH, jp);  //标签1北侧——>容器北侧
        springLayout.putConstraint(WEST, Admission_time, 25, WEST, jp);    //标签1西侧——>容器西侧 10
*/
        JButton Btn_time = new JButton("入院时间：");
        jp.add(Btn_time);
        springLayout.putConstraint(NORTH,Btn_time,30, NORTH, jp);
        springLayout.putConstraint(EAST,Btn_time,100, WEST, jp);

        Btn_time.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Point p=new Point(700,290);
                JTimeChooser chooser = new JTimeChooser(main,"选择时间",p,21);
                Calendar d = chooser.showTimeDialog();
                JLabel l1 = new JLabel("-----");
                jp.add(l1);
                l1.setText(d.get(Calendar.YEAR) + "-"
                        + (d.get(Calendar.MONTH) + 1) + "-"
                        + d.get(Calendar.DAY_OF_MONTH)
                        +"  "+d.get(Calendar.HOUR_OF_DAY)
                        +":"+d.get(Calendar.MINUTE)
                        +":"+d.get(Calendar.SECOND));
            }
        });

        JButton Btn_time1 = new JButton("出院时间：");
        jp.add(Btn_time1);
        springLayout.putConstraint(NORTH,Btn_time1,30, NORTH, jp);
        springLayout.putConstraint(EAST,Btn_time1,200, WEST, jp);

        Btn_time1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Point p=new Point(700,290);
                JTimeChooser chooser = new JTimeChooser(main,"选择时间",p,21);
                Calendar d = chooser.showTimeDialog();
                JLabel l1 = new JLabel("-----");
                jp.add(l1);
                l1.setText(d.get(Calendar.YEAR) + "-"
                        + (d.get(Calendar.MONTH) + 1) + "-"
                        + d.get(Calendar.DAY_OF_MONTH)
                        +"  "+d.get(Calendar.HOUR_OF_DAY)
                        +":"+d.get(Calendar.MINUTE)
                        +":"+d.get(Calendar.SECOND));
            }
        });


// 表头（列名）
        String[] columnNames = new String[]{"患者编号", "姓名", "性别", "年龄", "入院时间", "出院时间","状态"};
        Object[][] rowData = QueryInformationDataBase1.PatientInformation(1,10);

        /*
        // 表格所有行数据
        Object[][] rowData = {
                {"00001", "小明", "男", "18", 2022-11-30,0,0},
                {"00002", "小明", "女", "19", 2022-11-30,0,0},
                {"00003", "小明", "男", "20", 2022-11-30,0,0},
                {"00004", "小明", "女", "21", 2022-11-30,0,0},
                {"00005", "小明", "男", "22", 2022-11-30,0,0},
                {"00006", "小明", "女", "23", 2022-11-30,0,0},
        };
*/
        // 创建一个表格，指定 所有行数据 和 表头
        JTable table = new JTable( rowData, columnNames);

        // 把 表头 添加到容器顶部（使用普通的中间容器添加表格时，表头 和 内容 需要分开添加）
        jp.add(table.getTableHeader());
        // 把 表格内容 添加到容器中心
        jp.add(table);
        springLayout.putConstraint(NORTH,table,160, NORTH, jp);
        springLayout.putConstraint(EAST,table,550, WEST, jp);
        springLayout.putConstraint(NORTH,table.getTableHeader(),140, NORTH, jp);
        springLayout.putConstraint(EAST,table.getTableHeader(),550, WEST, jp);


        JButton search = new JButton("查询");
        jp.add(search);
        springLayout.putConstraint(NORTH,search,70, NORTH, jp);
        springLayout.putConstraint(EAST,search,450, WEST, jp);


        JButton Reset = new JButton("重置");
        jp.add(Reset);
        springLayout.putConstraint(NORTH,Reset,70, NORTH, jp);
        springLayout.putConstraint(EAST,Reset,520, WEST, jp);

        JButton add = new JButton("增加");
        jp.add(add);
        springLayout.putConstraint(NORTH,add,400, NORTH, jp);
        springLayout.putConstraint(EAST,add,400, WEST, jp);


        JButton modify = new JButton("修改");
        jp.add(modify);
        springLayout.putConstraint(NORTH,modify,400, NORTH, jp);
        springLayout.putConstraint(EAST,modify,470, WEST, jp);

        JButton delete = new JButton("删除");
        jp.add(delete);
        springLayout.putConstraint(NORTH,delete,400, NORTH, jp);
        springLayout.putConstraint(EAST,delete,530, WEST, jp);


        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setVisible(true);  // 显示窗口
    }

    public class JTimeChooserTest extends JFrame {

        private static final long serialVersionUID = 1L;

        private JFrame main;

        public JTimeChooserTest() {

            main = this;
            this.setTitle("测试JTimeChooser");

            final JLabel l1 = new JLabel("-----");
            this.add(l1, BorderLayout.SOUTH);

            JButton b1 = new JButton("选择时间");
            b1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Point p=new Point(700,290);
                    JTimeChooser chooser = new JTimeChooser(main,"选择时间",p,21);
                    Calendar d = chooser.showTimeDialog();
                    l1.setText(d.get(Calendar.YEAR) + "-"
                            + (d.get(Calendar.MONTH) + 1) + "-"
                            + d.get(Calendar.DAY_OF_MONTH)
                            +"  "+d.get(Calendar.HOUR_OF_DAY)
                            +":"+d.get(Calendar.MINUTE)
                            +":"+d.get(Calendar.SECOND));
                }
            });
            this.add(b1, BorderLayout.CENTER);

            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setBounds(300, 200, 600, 400);
            this.setVisible(true);
        }

        /**
         * 设置UI 默认为windows
         */
        /**
         * javax.swing.plaf.metal.MetalLookAndFeel
         * com.sun.java.swing.plaf.motif.MotifLookAndFeel
         * com.sun.java.swing.plaf.windows.WindowsLookAndFeel
         * com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
         *
         */
        private void setUI() {
            try {
                UIManager
                        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                JFrame.setDefaultLookAndFeelDecorated(true);
            } catch (Exception e) {
                System.out.println("Exception UI: " + e.getMessage());
            }
        }

    }

}
